package com.qmscy.sparrow.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * user details
 *
 * @Author: wangxiaonan
 * @Date: 2019/1/6
 **/
@Entity
@Table(name = "sqr_user")
@Data
@EqualsAndHashCode(callSuper = true)
public class User extends BaseEntity implements Serializable, UserDetails {

    private static final long serialVersionUID = 2507340730751848784L;

    @Column(name = "f_username", unique = true)
    private String username;
    @Column(name = "f_password")
    private String password;
    @Column(name = "f_email", unique = true)
    private String email;
    @Column(name = "f_real_name")
    private String realName;
    @Column(name = "f_gender")
    private String gender;
    @Column(name = "f_mobile", unique = true)
    private String mobile;
    @Column(name = "f_rank")
    private Integer rank;
    @Column(name = "f_type")
    private Integer type;
    @Column(name = "f_status")
    private Integer status;

    /**
     * 用户所拥有的权限
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 这里设置权限和角色
        ArrayList<GrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        authorities.add(new SimpleGrantedAuthority("AUTH_WRITE"));
        return authorities;
    }

    /**
     * 用户的账号是否过期,过期的账号无法通过授权验证. true 账号未过期
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 用户的账户是否被锁定,被锁定的账户无法通过授权验证. true 账号未锁定
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 用户的凭据(pasword) 是否过期,过期的凭据不能通过验证. true 没有过期,false 已过期
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否可用
     */
    @Override
    public boolean isEnabled() {
        return true;
    }

    public boolean isAdmin() {
        return false;
    }
}
